package cn.tedu.ivoscm.user.service.impl;

import cn.tedu.ivoscm.base.exception.ServiceException;
import cn.tedu.ivoscm.base.response.StatusCode;
import cn.tedu.ivoscm.user.mapper.UserMapper;
import cn.tedu.ivoscm.user.pojo.dto.UserLoginParam;
import cn.tedu.ivoscm.user.pojo.dto.UserQuery;
import cn.tedu.ivoscm.user.pojo.dto.UserSaveParam;
import cn.tedu.ivoscm.user.pojo.entity.User;
import cn.tedu.ivoscm.user.pojo.vo.UserVO;
import cn.tedu.ivoscm.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserVO login(UserLoginParam userLoginParam) {
        log.debug("用户登录业务:{}", userLoginParam);
        UserVO userVO = userMapper.selectByUsername(userLoginParam.getUsername());
        /* 如果业务层遇到了问题,不能直接返回结果给前端,而是要抛出业务层异常,让全局异常处理器来处理! */
        if (userVO == null) {
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!userVO.getPassword().equals(userLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        log.debug("用户查询结果：{}", userVO);
        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam usp) {
        log.debug("保存用户业务：{}", usp);
        User user = new User();
        BeanUtils.copyProperties(usp, user);
        if (user.getId() == null) {
            user.setPassword("123456");  // 后台赋予用户默认密码
            user.setCreateTime(new Date());
            userMapper.insert(user);
        } else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }
    }

    @Override
    public List<UserVO> list(UserQuery userQuery) {
        log.debug("查询用户列表业务：{}", userQuery);
        List<UserVO> list = userMapper.selectUser(userQuery);
        if (list == null) {
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
        return list;
    }

    @Override
    public List<UserVO> listAudit(Long parentId) {
        log.debug("查询审批人列表业务：{}", parentId);
        List<UserVO> list = new ArrayList<>();
        UserQuery uq = new UserQuery();
        uq.setId(parentId);
        List<UserVO> listMedia1 = userMapper.selectUser(uq);
        if (!listMedia1.isEmpty()) {
            list.add(listMedia1.get(0));
            Long ppId = listMedia1.get(0).getParentId();
            log.debug("上级领导的上级领导id：{}", ppId);
            if (ppId != null) {
                uq.setId(ppId);
                List<UserVO> listMedia2 = userMapper.selectUser(uq);
                if (!listMedia2.isEmpty()) {
                    list.add(listMedia2.get(0));
                }
            }
        }
        log.debug("查询审批人列表结果：{}", list);
        return list;
    }

    @Override
    public void updatePassword(Long id) {
        log.debug("重置密码业务：{}", id);
        User user = new User();
        user.setId(id);
        user.setPassword("root");
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void updateStatus(Long id, String status) {
        log.debug("修改账号状态业务：id: {}, status: {}", id, status);
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("删除用户业务：{}", id);
        userMapper.deleteById(id);
    }
}
